﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Util
{
    /// <summary>
    /// Some simple matrix and vector manipulation methods.
    /// </summary>
    public class MatrixUtils
    {
        public static String df = "0.00";


        public static String ToString(double[][] m) 
        {
            StringBuilder s = new StringBuilder("[");

            foreach (double[] row  in m) 
            {
                s.Append(ToString(row));
                s.Append('\n');
            }

            return s.Append(" ]").ToString();
        }


        public static String ToString(double[] m) 
        {
            StringBuilder s = new StringBuilder("[");

            foreach (double val in m) 
            {
                s.Append(' ').Append(String.Format(df,val));
            }

            return s.Append(" ]").ToString();
        }


        public static int numCols(double[][] m) 
        {
            return m[0].Length;
        }


        public static String toString(float[][] matrix) 
        {
            return ToString(float2double(matrix));
        }


        public static float[] double2float(double[] values) 
        { // what a mess !!! -> fixme: how to convert number arrays ?
            float[] newVals = new float[values.Length];
            for (int i = 0; i < newVals.Length; i++) 
            {
                newVals[i] = (float) values[i];
            }

            return newVals;
        }


        public static float[][] double2float(double[][] array) 
        {
            float[][] floatArr = new float[array.Length][];
            for (int i = 0; i < array.Length; i++)
                floatArr[i] = double2float(array[i]);

            return floatArr;
        }


        public static double[] float2double(float[] values) 
        {
            double[] doubArr = new double[values.Length];
            for (int i = 0; i < doubArr.Length; i++)
                doubArr[i] = values[i];

            return doubArr;
        }


        public static double[][] float2double(float[][] array) 
        {
            double[][] doubArr = new double[array.Length][];
            for (int i = 0; i < array.Length; i++)
                doubArr[i] = float2double(array[i]);

            return doubArr;
        }
    }
}
